package jm.nj.demo22mian.curd;

import jm.nj.demo22mian.utils.JdbcUitils;
import org.junit.Test;

import java.sql.*;

public class JdbcCurdDemo {

    public void demo(){
        Connection conn = null;
        Statement stmt= null;
        ResultSet rs = null;
        try {
            conn = JdbcUitils.getConnection();
            stmt = conn.createStatement();
            //执行其他操作...
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            JdbcUitils.closeResource(conn, stmt, rs);
        }
    }


    @Test
    public void update(){
        Connection conn = null;
        Statement stmt= null;
        //ResultSet rs = null;
        try {
            conn = JdbcUitils.getConnection();
            stmt = conn.createStatement();
            //执行其他操作...
            int count = stmt.executeUpdate("INSERT INTO USER(username,PASSWORD,salary,age,registTime)VALUE('tom','123',5000,50,'2016/11/10')");
            //结果
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            JdbcUitils.closeResource(conn, stmt, null);
        }
    }


    @Test
    public void query(){
        Connection conn = null;
        Statement stmt= null;
       ResultSet rs = null;
        try {
            conn = JdbcUitils.getConnection();
            stmt = conn.createStatement();
            //执行其他操作...
            rs = stmt.executeQuery("select * from user ");
            while(rs.next()){
                Integer uid = rs.getInt("uid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Double salary = rs.getDouble("salary");
                Integer age = rs.getInt("age");
                Date registTime = rs.getDate("registTime");
                Timestamp lastupdate = rs.getTimestamp("lastupdate");
                System.out.println(uid + "  " + username + "  " + password + "  " + salary + "  " +age + "  " + registTime + "  " + lastupdate);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            JdbcUitils.closeResource(conn, stmt, rs);
        }
    }
    @Test
    public void findById(){
        Connection conn = null;
        Statement stmt= null;
        ResultSet rs = null;
        try {
            conn = JdbcUitils.getConnection();
            stmt = conn.createStatement();
            //执行其他操作...
            rs = stmt.executeQuery("select * from USER where uid = 1");
            while(rs.next()){
                Integer uid = rs.getInt("uid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Double salary = rs.getDouble("salary");
                Integer age = rs.getInt("age");
                Date registTime = rs.getDate("registTime");
                Timestamp lastupdate = rs.getTimestamp("lastupdate");
                System.out.println(uid + "  " + username + "  " + password + "  " + salary + "  " +age + "  " + registTime + "  " + lastupdate);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            JdbcUitils.closeResource(conn, stmt, rs);
        }
    }
    @Test
    public void delete(){
        Connection conn = null;
        Statement stmt= null;
        //ResultSet rs = null;
        try {
            conn = JdbcUitils.getConnection();
            stmt = conn.createStatement();
            //执行其他操作...
            int count = stmt.executeUpdate("delete from user where uid = 1");
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            JdbcUitils.closeResource(conn, stmt, null);
        }
    }



}
